﻿using Newtonsoft.Json;
using SHKF.B2C.MeiTuanPeiSong.Model;
using SHKF.B2C.MeiTuanPeiSong.Model.Request;
using SHKF.B2C.Model;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
 

namespace SHKF.B2C.MeiTuanPeiSong.Network
{
    public class ParamBuilder
    {
        public static Dictionary<string, string> buildParams(AbstractRequest request)
        { 
            request.appkey = OpenApiConfig.APP_KEY;
            request.timestamp = DateTimeUtil.DateTimeToTimeStamp(DateTime.Now);
            request.sign = "";

            Dictionary<string, string> list = new Dictionary<string, string>();
            foreach (PropertyInfo property in request.GetType().GetProperties())
            {
                if (property.Name == "SN")
                {
                    continue;
                }

                object value = property.GetValue(request);
                if (value != null)
                { 
                    string strValue = "";

                    if (value is IJson)
                    {
                        strValue = JsonConvert.SerializeObject(value);
                    }
                    else if (value != null)
                    {
                        if ( (value is DateTime) && ((DateTime)value).Year == 1900)
                        {
                            continue;   
                        }
                        
                        strValue = value.ToString();
                    }

                    if (!string.IsNullOrEmpty(strValue))
                    {
                        list.Add(property.Name, strValue);
                    }
                }

            }

            return list;
        }
    }
}
